package com.project;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端类
 */
public class Client {
    public Client(){
//        try {
////            链接指定的IP和端口的服务器
//            Socket socket =new Socket("127.0.0.1",8088);
//            //               从socket中得到读取流，用于读取客户端发送的数据
//            InputStream in = socket.getInputStream();
////                从socket中得到写入流，用于将数据发送给客户端
//            OutputStream out = socket.getOutputStream();
////            向服务器发送数据
//            out.write("hello".getBytes());
//            out.flush();
//
//            byte[] by = new byte[1024];
////                将读取流中的数据，读取到byte数组中
//            in.read(by);
////                将byte数组，转换为String
//            String str = new String(by);
//
//            System.out.println("服务器发送:"+str);
//            //                关闭流
//            out.close();
//            in.close();
//            socket.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        try {
//            链接到指定的IP和端口
            Socket socket = new Socket("127.0.0.1",8088);

            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();

            out.write("hello".getBytes());
            out.flush();

            byte [] by = new byte[1024];

            in.read(by);

            String str = new String(by).trim();

            System.out.println("服务器发出:"+str);

            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Client();
    }
}
